package cn.edu.lit.jsjxy.CourseExample.chapter2;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.lit.jsjxy.CourseExample.R;

public class ListViewTest2Activity extends Activity
{

    ListView listview;
    String[] items;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view_test2);

        listview = findViewById(R.id.list1);

        View headerView = new View(this);
        View footerView = new View(this);

        headerView.setBackgroundColor(Color.RED);
        footerView.setBackgroundColor(Color.RED);

        headerView.setMinimumHeight(10);
        footerView.setMinimumHeight(10);

        listview.addHeaderView(headerView);
        listview.addFooterView(footerView);

        items = initData();

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_single_choice,
            android.R.id.text1, items);

        listview.setAdapter(adapter);
        //setListAdapter(adapter);

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
            {
                TextView tv = (TextView) view;
                Toast.makeText(ListViewTest2Activity.this, tv.getText(),
                        Toast.LENGTH_SHORT).show();
            }
        });


    }

    public String[] initData()
    {
        Resources res = getResources();


        String[] data = res.getStringArray(R.array.listItems);
        return data;

    }
}
